#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, res1 = -1, res2 = -1, res = INT_MAX;
    cin >> n;
    vector<string>str(n);
    string str1, str2;
    cin >> str1 >> str2;
    for (int i = 0; i < n; i++)
        cin >> str[i];
    for (int i = 0; i < n; i++)
    {
        if (str[i] == str1)
        {
            res1 = i;
            if (res2 != -1)
                res = min(res, abs(res1 - res2));
        }
        else if (str[i] == str2)
        {
            res2 = i;
            if (res1 != -1)
                res = min(res, abs(res1 - res2));
        }
    }
    if (res1 == -1 || res2 == -1)
        cout << -1 << endl;
    else
        cout << res << endl;
    return 0;
}